package com.gist.cithub.comment.service.impl;

import com.gist.cithub.comment.dao.ReplyDao;
import com.gist.cithub.comment.entity.Comment;
import com.gist.cithub.comment.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gist.common.utils.PageUtils;
import com.gist.common.utils.Query;

import com.gist.cithub.comment.dao.RootDao;
import com.gist.cithub.comment.entity.RootEntity;
import com.gist.cithub.comment.service.RootService;


@Service("rootService")
public class RootServiceImpl extends ServiceImpl<RootDao, RootEntity> implements RootService {
    @Autowired
    RootDao rootDao;
    @Autowired
    ReplyService replyService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RootEntity> page = this.page(
                new Query<RootEntity>().getPage(params),
                new QueryWrapper<RootEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Comment> getCommentsByServiceId(Integer serviceid) {
        List<Comment>list=new ArrayList<>();
        QueryWrapper<RootEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.and(
                wrapper ->
                        wrapper.eq("serviceid",serviceid)
        );
        List<RootEntity> rootEntityList=rootDao.selectList(queryWrapper);
        for (int i = 0; i <rootEntityList.size() ; i++) {
            Comment comment=new Comment();
            comment.setShowReply(false);
            comment.setRoot(rootEntityList.get(i));
            comment.setReply(replyService.getCommentsByRootId(rootEntityList.get(i).getId()));
            list.add(comment);
        }
        return list;
    }

}